package games;

import java.util.Random;
import java.util.Scanner;

/**
 * 黑白棋：一个简单的 3x3 黑白棋，电脑随机走棋，没有智能。
 *
 * @author 何老师
 */
public class SimpleTicTacToe {
    private static final char HUMAN = 'X';          // 玩家符号
    private static final char COMPUTER = 'O';       // 电脑符号
    private static final char EMPTY = ' ';          // 空位符号
    private static final char[][] board = new char[3][3]; // 3x3 棋盘
    private static final Scanner scanner = new Scanner(System.in);
    private static final Random random = new Random();

    public static void main(String[] args) {
        // 初始化棋盘
        initializeBoard();
        boolean humanTurn = true; // 玩家先手

        // 游戏开始提示
        System.out.println("欢迎体验3x3井字棋！你用'X'，电脑用'O'。输入行和列（0-2）落子。");

        // 主游戏循环
        while (true) {
            printBoard(); // 显示棋盘
            if (humanTurn) {
                humanMove(); // 玩家落子
                if (checkWin(HUMAN)) {
                    printBoard();
                    System.out.println("你赢了！");
                    break;
                }
            } else {
                computerMove(); // 电脑落子
                if (checkWin(COMPUTER)) {
                    printBoard();
                    System.out.println("电脑赢了！");
                    break;
                }
            }
            if (isBoardFull()) {
                printBoard();
                System.out.println("平局！");
                break;
            }
            humanTurn = !humanTurn; // 切换回合
        }
        scanner.close();
    }

    // 初始化棋盘，将所有位置设为空
    private static void initializeBoard() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                board[i][j] = EMPTY;
            }
        }
    }

    // 显示当前棋盘状态
    private static void printBoard() {
        System.out.println("    0   1   2"); // 列号
        System.out.println("  +---+---+---+");
        for (int i = 0; i < 3; i++) {
            System.out.print(i + " | ");
            for (int j = 0; j < 3; j++) {
                System.out.print(board[i][j] + " | ");
            }
            System.out.println();
            System.out.println("  +---+---+---+");
        }
        System.out.println();
    }

    // 玩家落子
    private static void humanMove() {
        int row, col;
        while (true) {
            System.out.print("请输入行和列（例如：1 1）：");
            row = scanner.nextInt();
            col = scanner.nextInt();
            // 检查输入是否合法
            if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == EMPTY) {
                board[row][col] = HUMAN;
                break;
            }
            System.out.println("非法位置，请重新输入！");
        }
    }

    // 电脑落子（随机选择空位）
    private static void computerMove() {
        while (true) {
            int row = random.nextInt(3);
            int col = random.nextInt(3);
            if (board[row][col] == EMPTY) {
                board[row][col] = COMPUTER;
                System.out.println("电脑落子在：" + row + " " + col);
                break;
            }
        }
    }

    // 检查某玩家是否获胜
    private static boolean checkWin(char player) {
        // 检查行
        for (int i = 0; i < 3; i++) {
            if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
                return true;
            }
        }
        // 检查列
        for (int i = 0; i < 3; i++) {
            if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
                return true;
            }
        }
        // 检查对角线
        if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
            return true;
        }
        return board[0][2] == player && board[1][1] == player && board[2][0] == player;
    }

    // 检查棋盘是否已满
    private static boolean isBoardFull() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] == EMPTY) {
                    return false;
                }
            }
        }
        return true;
    }
}